package tyk.Service;

import tyk.Dao.BaseDao;
import tyk.pojo.AccountPage;
import tyk.pojo.Merchant;
import tyk.pojo.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title AccountManagementService
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/23 1:01
 **/
public class AccountManagementService extends BaseDao {
    public List<Merchant> SelectAccountManagementByPage(Integer Page, AccountPage accountPage) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        List<Merchant> list=new ArrayList<>();
        String sql="select user_id,user_name,cnt from merchant limit ?,?;";
        String sql2="select user_id,user_name,cnt from merchant;";
        list=super.executeQuery(Merchant.class,sql,(Page-1)*12,12);
        accountPage.setCount(super.executeQuery(Merchant.class,sql2).size());
        accountPage.setPageTocal(accountPage.getCount()/12);
        if(accountPage.getCount()%12!=0) accountPage.setPageTocal(accountPage.getPageTocal()+1);
        return list;
    }
    public Merchant EditAccountManagementById(Integer Id,Integer page) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        String sql="select user_id,user_name,cnt from merchant where user_id = ?;";
        Merchant merchant =super.executeQuery(Merchant.class,sql,Id).get(0);
        return merchant;
    }
    public void DeleteAccountManagementById(Integer Id) throws SQLException {
        String sql="delete from merchant where user_id = ?";
        super.executeUpdate(sql,Id);
    }
    public void EditAccount(Integer user_id,Integer cnt) throws SQLException {
        String sql="update merchant set cnt = ? where user_id = ?;";
        super.executeUpdate(sql,cnt,user_id);
    }
    public Integer SelectAccountByUser_name(String user_name) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        System.out.println("到此二游");
        List<User> list=new ArrayList<>();
        String sql="select id,username,password,email from user where username = ?;";
        User user=super.executeQuery(User.class,sql,user_name).get(0);
        System.out.println(user);
        return user.getId();
    }
    public void AddAccount(Merchant merchant) throws SQLException {
        String sql="insert into merchant (user_id,user_name,cnt) values(?,?,?);";
        super.executeUpdate(sql,merchant.getUser_id(),merchant.getUser_name(),merchant.getCnt());
    }
}
